*** to be run after 1_Table_2\5_convexity

clear
version 13
set more off 
// global paths 
global datainpath "/ssb/stamme04/ekstern/apa/wk24/leveranse/"
global dataoutpath "/ssb/stamme04/ekstern/wia/wk48/"
global graphpath "/ssb/stamme04/ekstern/apa/prog/apa/" 
global outputpath "/ssb/stamme04/ekstern/apa/prog/apa/" 

cap log close
log  using  .../AA_5_exp_04Sep2020.smcl, replace


use .../left_cens_spell_10sample_10never_type_1_conv_Sept2020.dta, clear
								  


***tax convexity parameter
capture drop __000000 __000001 __000002 __000003 __000004 __000005 __000006 __00000A __000008 __000009 __00000B __00000C
set more off 

gen everself1=lm_status==1
bysort id: egen everself1sum=max(everself1)

gen pweight = 1
replace pweight = 10  if everself1sum == 0 

*keep if year == 2000

set seed 210385
global draws 200

forvalues r=1/$draws {
 gen double random_naer_flat_`r'=rnormal(pred_lnnaer,sqrt(var_naer))
 replace random_naer_flat_`r'=exp(random_naer_flat_`r')
 }
 egen double random_naer_flat_0=rowmean(random_naer_flat_1-random_naer_flat_$draws)
 
forvalues r=0/$draws {
foreach inc in random_naer_flat_`r'  {
gen tax_flat_`inc'=0
}
}

set more off
foreach y of num 2000/2000 {
forvalues r=0/$draws {
 
      	   tempvar gross gross1 alm0 alm1 xss xkom  xsta name b b1 z dedu k

	  gen double  `gross' = 0
          gen double `gross1' = 0
          gen double   `ordinary0' = 0
          gen double   `ordinary1' = 0
          gen double    `sic' = 0
          gen double   `flat' = 0
          gen double   `surtax' = 0
	  gen double   `z' = 0
          gen double   `b'= random_naer_flat_`r'
          gen double   `b1'= `b'*1
	  gen double   `dedu' = 0
	  gen double  `k'=capital_income
	  
	*-* Deductions 
			    
	*-* Tax base and calculated taxes

	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu' , 0)  if year==`y'                         // k is capital   
	replace `ordinary1' = max(`ordinary0' - 27700 , 0)  if year==`y'


	replace `sic' = max(min([.078*`z'+.107*`b1']*(`gross1'>22200) , 0.25*[`gross1'-22200]) , 0)  if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.28  if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-166500,0)*.08   if year==`y'                 // surtax, use imputed personal for business

	*-* Total income tax 
	replace tax_flat_random_naer_flat_`r' = `sic' + `flat'+`surtax'    if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'

	  
      
      
      
      
   
} 
} 



egen tax_flat_naer_exp_flat = rowmean(tax_flat_random_naer_flat_1-tax_flat_random_naer_flat_$draws )
***** rifare con correzione per average_randiom naer
gen average_random_naer_flat=random_naer_flat_0- tax_flat_random_naer_flat_0
replace average_random_naer_flat=random_naer_flat_0- tax_flat_random_naer_flat_0 + capital_income if capital_income>0
gen convexity_naer_flat= (ln(tax_flat_naer_exp_flat/tax_flat_random_naer_flat_0))*(tax_flat_random_naer_flat_0/average_random_naer_flat)

drop random_naer_flat_1-random_naer_flat_200
drop tax_flat_random_naer_flat_1-tax_flat_random_naer_flat_200


save .../left_cens_spell_10sample_10never_type_flat_2000_1_Sep2020.dta,replace

log close
